﻿using BaseUi.Device;
using BaseUi.Models;
using BaseUi.ViewModels;
using HandyControl.Controls;
using LiveCharts;
using MiniExcelLibs;
using Ping9719.DeviceCommunication;
using Ping9719.DeviceCommunication.Clients.PLC;
using Ping9719.WpfEx;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Win32;

namespace BaseUi.ViewModel
{
    public class HomeViewModel : BindableBase
    {
        /// <summary>
        /// 4个位置上是否有料
        /// </summary>
        int[] textnum = new[] { 0, 0, 0, 0 };
        /// <summary>
        /// 4个位置上的OKNG情况
        /// </summary>
        string[] textokng = new[] { "no", "no", "no", "no" };
        /// <summary>
        /// 4个位置上的最终结果
        /// </summary>
        string[] textend = new[] { "", "", "", "" };
        /// <summary>
        /// 转盘方向
        /// </summary>
        int direction = 0;
        /// <summary>
        /// 下压气缸
        /// </summary>
        bool cylinder = false;
        /// <summary>
        /// 启动方式
        /// </summary>
        public int runstart = 0;
        /// <summary>
        /// 启动旋转方向
        /// </summary>
        public int way = 0;
        /// <summary>
        /// 转盘状态
        /// </summary>
        public bool directioning = false;
        /// <summary>
        /// 转盘状态
        /// </summary>
        public bool Working = false;

        private Task[] woektasks = new Task[2];
        //Task woekstart = new Task;
        private Task[] task1 = new Task[2];
        private Task[] task2 = new Task[2];
        private Task[] task3 = new Task[2];
        private Task[] task4 = new Task[2];

        CancellationTokenSource cancellationToken = null;//停止
        ManualResetEvent manualReset = null;//暂停、继续
        WarnInfoContainer WarnInfo = new WarnInfoContainer();//警告、错误等信息
        #region 页面
        private string text1_ = "等待";
        public string text1 { get => text1_; set { SetProperty(ref text1_, value); } }
        private string text2_ = "等待";
        public string text2 { get => text2_; set { SetProperty(ref text2_, value); } }
        private string text3_ = "等待";
        public string text3 { get => text3_; set { SetProperty(ref text3_, value); } }
        private string text4_ = "等待";
        public string text4 { get => text4_; set { SetProperty(ref text4_, value); } }
        private Brush ZB1_ = Brushes.Gainsboro;
        public Brush ZB1 { get => ZB1_; set { SetProperty(ref ZB1_, value); } }
        private Brush ZB2_ = Brushes.Gainsboro;
        public Brush ZB2 { get => ZB2_; set { SetProperty(ref ZB2_, value); } }
        private Brush ZB3_ = Brushes.Gainsboro;
        public Brush ZB3 { get => ZB3_; set { SetProperty(ref ZB3_, value); } }
        private Brush ZB4_ = Brushes.Gainsboro;
        public Brush ZB4 { get => ZB4_; set { SetProperty(ref ZB4_, value); } }
        public void SetUi()
        {
            if (direction == 1)
            {
                if (textnum[0] == 1)
                {
                    if (textokng[0] == "no")
                    {
                        text1 = "1号未测";
                        ZB1 = Brushes.DarkTurquoise;
                    }
<<<<<<< HEAD
                    else if (textokng[0] == "okend")
=======
                    else if (textokng[0] == "ok13")
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                    {
                        text1 = "1号OK";
                        ZB1 = Brushes.ForestGreen;
                    }
                    else if(textokng[0].Contains("ok"))
                    {
                        text1 = "1号测试中";
                        ZB1 = Brushes.DarkTurquoise;
                    }
                    else 
                    {
                        text1 = "1号失败";
                        ZB1 = Brushes.Crimson;
                    }
                }
                else if(textnum[0] == 0)
                {
                    text1 = "1号无料";
                    ZB1 = Brushes.Gainsboro;
                }
                else
                {
                    text1 = "1号异常";
                    ZB1 = Brushes.Crimson;
                }
                if (textnum[1] == 1)
                {
                    if (textokng[1] == "no")
                    {
                        text2 = "2号未测";
                        ZB2 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[1] == "ok11")
                    {
                        text2 = "2号OK";
                        ZB2 = Brushes.ForestGreen;
                    }
                    else if (textokng[1].Contains("ok"))
                    {
                        text2 = "2号测试中";
                        ZB2 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text2 = "2号失败";
                        ZB2 = Brushes.Crimson;
                    }
                }
                else if(textnum[1] == 0)
                {
                    text2 = "2号无料";
                    ZB2 = Brushes.Gainsboro;
                }
                else
                {
                    text2 = "2号感应异常";
                    ZB2 = Brushes.Crimson;
                }

                if (textnum[2] == 1)
                {
                    if (textokng[2] == "no")
                    {
                        text3 = "3号未测";
                        ZB3 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[2] == "ok11")
                    {
                        text3 = "3号OK";
                        ZB3 = Brushes.ForestGreen;
                    }
                    else if (textokng[2].Contains("ok"))
                    {
                        text3 = "3号测试中";
                        ZB3 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text3 = "3号失败";
                        ZB3 = Brushes.Crimson;
                    }
                }
                else if(textnum[2] == 0)
                {
                    text3 = "3号无料";
                    ZB3 = Brushes.Gainsboro;
                }
                else
                {
                    text3 = "3号感应异常";
                    ZB3 = Brushes.Crimson;
                }
            
                if (textnum[3] == 1)
                {
                    if (textokng[3] == "no")
                    {
                        text4 = "4号未测";
                        ZB4 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[3] == "ok11")
                    {
                        text4 = "4号OK";
                        ZB4 = Brushes.ForestGreen;
                    }
                    else if (textokng[3].Contains("ok"))
                    {
                        text4 = "4号测试中";
                        ZB4 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text4 = "4号失败";
                        ZB4 = Brushes.Crimson;
                    }
                }
                else if(textnum[3] ==0)
                {
                    text4 = "4号无料";
                    ZB4 = Brushes.Gainsboro;
                }
                else
                {
                    text4 = "4号感应异常";
                    ZB4 = Brushes.Crimson;
                }

            }
            else if (direction == 2)
            {
                if (textnum[2] == 1)
                {
                    if (textokng[2] == "no")
                    {
                        text1 = "3号未测";
                        ZB1 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[2] == "ok11")
                    {
                        text1 = "3号OK";
                        ZB1 = Brushes.ForestGreen;
                    }
                    else if (textokng[2].Contains("ok"))
                    {
                        text1 = "3号测试中";
                        ZB1 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text1 = "3号失败";
                        ZB1 = Brushes.Crimson;
                    }
                }
                else if(textnum[2] ==0)
                {
                    text1 = "3号无料";
                    ZB1 = Brushes.Gainsboro;
                }
                else
                {
                    text1 = "3号感应异常";
                    ZB1 = Brushes.Crimson;
                }
                if (textnum[3] == 1)
                {
                    if (textokng[3] == "no")
                    {
                        text2 = "4号未测";
                        ZB2 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[3] == "ok11")
                    {
                        text2 = "4号OK";
                        ZB2 = Brushes.ForestGreen;
                    }

                    else if (textokng[3].Contains("ok"))
                    {
                        text2 = "4号测试中";
                        ZB2 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text2 = "4号失败";
                        ZB2 = Brushes.Crimson;
                    }
                }
                else if(textnum[3] ==0)
                {
                    text2 = "4号无料";
                    ZB2 = Brushes.Gainsboro;
                }
                else
                {
                    text2 = "4号感应异常";
                    ZB2 = Brushes.Crimson;
                }
                if (textnum[0] == 1)
                {
                    if (textokng[0] == "no")
                    {
                        text3 = "1号未测";
                        ZB3 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[0] == "ok11")
                    {
                        text3 = "1号OK";
                        ZB3 = Brushes.ForestGreen;
                    }
                    else if (textokng[0].Contains("ok"))
                    {
                        text3 = "1号测试中";
                        ZB3 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text3 = "1号失败";
                        ZB3 = Brushes.Crimson;
                    }
                }
                else if(textnum[0] ==0)
                {
                    text3 = "1号无料";
                    ZB3 = Brushes.Gainsboro;
                }
                else
                {
                    text3 = "1号感应异常";
                    ZB3 = Brushes.Crimson;
                }
                if (textnum[1] == 1)
                {
                    if (textokng[1] == "no")
                    {
                        text4 = "2号未测";
                        ZB4 = Brushes.DarkTurquoise;
                    }
                    else if (textokng[1] == "ok11")
                    {
                        text4 = "2号OK";
                        ZB4 = Brushes.ForestGreen;
                    }
                    else if (textokng[1].Contains("ok"))
                    {
                        text4 = "2号测试中";
                        ZB4 = Brushes.DarkTurquoise;
                    }
                    else
                    {
                        text4 = "2号失败";
                        ZB4 = Brushes.Crimson;
                    }
                }
                else if (textnum[1] == 0)
                {
                    text4 = "2号无料";
                    ZB4 = Brushes.Gainsboro;
                }
                else
                {
                    text4 = "2号感应异常";
                    ZB4 = Brushes.Crimson;
                }
            }
            else
            {
                text1 = "null";
                text2 = "null";
                text3 = "null";
                text4 = "null";
                ZB1 = Brushes.Gainsboro;
                ZB2 = Brushes.Gainsboro;
                ZB3 = Brushes.Gainsboro;
                ZB4 = Brushes.Gainsboro;
            }
        }
        #endregion
        #region 绑定
        private string textErr;
        /// <summary>
        /// 错误文本
        /// </summary>
        public string TextErr { get => textErr; set { SetProperty(ref textErr, value); } }
        //所有信息
        List<ErrInfo> errInfos = new List<ErrInfo>();
        struct ErrInfo
        {
            public bool IsTemp;//临时
            public string Name;//名称、错误组名
            public string ErrText;//错误信息
        }
        public ICommand ClearTextInfoCommand { get => new DelegateCommand(ClearTextInfo); }
        /// <summary>
        /// 清除信息文本
        /// </summary>
        public void ClearTextInfo()
        {
            TextBoxLog.ClearLog();
        }
        public ICommand ClearTextErrCommand { get => new DelegateCommand(ClearTextErr); }
        /// <summary>
        /// 清除全部错误文本
        /// </summary>
        public void ClearTextErr()
        {
            errInfos.Clear();
            UpdataErrToUi();
            //WarnInfo.RemoveAll(WarnInfoType.AlwayWarn);
        }
        /// <summary>
        /// 添加一个常驻错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTextErr(string err)
        {
            AddTextErr(new string[] { err });
        }

        /// <summary>
        /// 添加一个常驻错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTextErr(string name, string err)
        {
            AddTextErr(name, new string[] { err });
        }

        /// <summary>
        /// 添加一个常驻错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTextErr(IEnumerable<string> errs)
        {
            AddTextErr(string.Empty, errs);
        }

        /// <summary>
        /// 添加一个常驻错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTextErr(string name, IEnumerable<string> errs)
        {
            if (errs == null)
                return;

            foreach (var item in errs)
            {
                ErrInfo errInfo = new ErrInfo();
                errInfo.IsTemp = false;
                errInfo.Name = name;
                errInfo.ErrText = item;
                errInfos.Add(errInfo);
            }

            UpdataErrToUi();
        }

        /// <summary>
        /// 添加临时错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTempTextErr(IEnumerable<string> errs)
        {
            AddTempTextErr(string.Empty, errs);
        }

        /// <summary>
        /// 添加临时错误
        /// </summary>
        /// <param name="err"></param>
        public void AddTempTextErr(string name, IEnumerable<string> errs)
        {
            errInfos.RemoveAll(i => i.Name == name);

            if (errs != null && errs.Any())
                foreach (var item in errs)
                {
                    ErrInfo errInfo = new ErrInfo();
                    errInfo.IsTemp = true;
                    errInfo.Name = name;
                    errInfo.ErrText = item;
                    errInfos.Add(errInfo);
                }

            UpdataErrToUi();
        }

        //更新错误到界面
        private void UpdataErrToUi()
        {
            TextErr = string.Join(Environment.NewLine, errInfos?.OrderBy(o => (o.Name, o.IsTemp))?.Select(o => string.IsNullOrEmpty(o.Name) ? o.ErrText : $"[{o.Name}]{o.ErrText}") ?? new string[] { });
        }
        #endregion

        #region 页面加载时任务
        /// <summary>
        /// 页面第一次加载时执行的任务
        /// </summary>
        public void LoadTask()
        {
            //注册警告事件
            WarnInfo.WarnInfoChanged += (all, add, rem) =>
            {
                //界面展示
                TextErr = string.Join(Environment.NewLine, all.OrderBy(o => (o.WarnType, o.Source, o.Text)));
                //日志记录
                Logs.Write(add.Concat(rem).Select(o => o.ToString()), LogsType.Warning);
                //数据库保存
                //if (WarnInfoDb.IsEnabled)
                //{
                //    WarnInfoDb.db.Insertable(WarnInfoItemDb.GetList(add)).ExecuteCommand();
                //    WarnInfoDb.db.Updateable(WarnInfoItemDb.GetList(rem)).ExecuteCommand();
                //}
            };
            LocalStatic.Electric.s();
            TextBoxLog.AddLog("状态检测");
            way = 0;
            Start();
            Load();
            if (RunState == RunState.Run)
            {
                TextBoxLog.AddLog("状态检测完毕，可以运行");
                Task.Run(Circulate);
            }
            else
            {
                TextBoxLog.AddLog("状态检测完毕，未满足运行条件");
            }
            //Task.Run(Circulate);
            //Trial();
            //Task.Run(PlcErrListen);
        }
        /// <summary>
        /// 测试通信
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Trial()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据Excel里面的内容监听plc错误信息
        /// </summary>
        async void PlcErrListen()
        {
            //Excel解析的错误格式
            var excelErrAll = ExcelDeviceAlarmModel.GetDatas(LocalFile.PlcDotPath);
            while (true)
            {
                //需要将null替换为设备，如“LocalStatic.Plc”，并去里面解开注释
                var excelErr = ExcelDeviceAlarmModel.ReadPlcAlarm(excelErrAll, null);

                WarnInfo.AddOrUpdate(excelErr.Select(o => new WarnInfoItem()
                {
                    Text = o.名称,
                    WarnType = WarnInfoType.WhileWarn,
                    Level = o.级别,
                    Solution = o.解决方式
                }), "PLC");

                await Task.Delay(500);
            }
        }
        #endregion

        #region 业务

        private RunState runState = RunState.Stop;
        /// <summary>
        /// 运行状态
        /// </summary>
        public RunState RunState { get => runState; set { SetProperty(ref runState, value); } }

        public ICommand RunStateClickCommand { get => new DelegateCommand<RunStateButtonClick>(RunStateClick); }
        /// <summary>
        /// 运行状态
        /// </summary>
        public void RunStateClick(RunStateButtonClick clickrunState)
        {
            if (clickrunState == RunStateButtonClick.Start)
            {

                //BarChartView.viewModel.AddNg(1);
                //TextBoxLog.AddLog("点击开始");
                way = 0;
                if (RunState == RunState.Run)
                {
                    TextBoxLog.AddLog("正常状态禁止初始化");
                    return;
                    //Stop();
                }
<<<<<<< HEAD
                if (RunState != RunState.Stop) {
                    Stop();
                }
=======
                Stop();
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                Start();
                initial();
                Load();
                if (RunState == RunState.Run)
                {
                    TextBoxLog.AddLog("状态检测完毕，可以运行");
                    Task.Run(Circulate);
                }
                else
                {
                    TextBoxLog.AddLog("状态检测完毕，未满足运行条件");
                }
            }
            else if (clickrunState == RunStateButtonClick.Pause)
            {
                TextBoxLog.AddLog("点击暂停");
                Pause();
            }
            else if (clickrunState == RunStateButtonClick.Continue)
            {
                TextBoxLog.AddLog("点击继续");
                string retul = prepose();
                if (retul != "OK")
                {
                    TextBoxLog.AddLog(retul);
                    Pause();
                }
                if (way == 1)
                {
                    var ddd = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
                    if (!ddd.IsSucceed)
                    {
                        TextBoxLog.AddLog("旋转气缸失败" + ddd.Err);
                        return;
                    }
                }
                else if (way == 2)
                {
                    var ddd = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
                    if (!ddd.IsSucceed)
                    {
                        TextBoxLog.AddLog("旋转气缸失败" + ddd.Err);
                        return;
                    }
                }
                Continue();
            }
            else if (clickrunState == RunStateButtonClick.Stop)
            {
                TextBoxLog.AddLog("点击停止");
                Stop();
            }
            else if (clickrunState == RunStateButtonClick.Rset)
            {

            }
        }
        /// <summary>
        /// 获取初始信息判断状态 
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Load()
        {
            int number = 0;
            int dian = 0;
            while (true)
            {
<<<<<<< HEAD
                Thread.Sleep(100);
=======
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                number = number + 1;
                if (number>5) {
                    break;

                }
                dian = getfreque();
                if (dian != -1)
                {
                    break;
                }
            }
            if (dian != 1 && dian != 2)
            {
                TextBoxLog.AddLog("旋转气缸到位通信失败" + dian + "（-1通信失败，0两边都没有到位信号，1正向，2反向，3两边都有到位信息）");
<<<<<<< HEAD
                if (RunState != RunState.Stop) { Stop(); }
=======
                Stop();
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                return;
            }
            else
            {
                direction = dian;
            }
            directioning = false;
            string retul = getjack();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
<<<<<<< HEAD
                if (RunState != RunState.Stop) { Stop(); }
=======
                Stop();
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                return;
            }
            retul = "";
            for (int i = 0; i < 4; i++)
            {
                if (textnum[i] == 1)
                {
                    TextBoxLog.AddLog("测试" + (i+1).ToString() + "箱有产品");
                    retul = "NG";
                }
                else if (textnum[i] != 0&& textnum[i] !=1)
                {
                    TextBoxLog.AddLog("测试" + (i + 1).ToString() + "箱感应值异常");
                    retul = "NG";
                }
                textokng[i] = "no";
            }
            if (RunState != RunState.Stop&& retul!="") { Stop(); }
            return;
            //SetUi(); 

        }
        /// <summary>
        /// 初始化（使状态回到可以运行的状态）
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void initial()
        {
            direction = 1;
            string retul = "";
            retul = getjack();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                    return;
                }
<<<<<<< HEAD
                return;
            }
            retul = satisfy();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = prepose();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = turn(0);
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            Thread.Sleep(500);
            reach();
            Changeled(0);
            Changeled(1);
            Jack();
            Changend();
            //Thread.Sleep(1000);
            retul = prepose();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = turn(1);
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            Thread.Sleep(500);
            reach();
            Changeled(0);
            Changeled(1);
            Jack();
            Changend();
=======
                else if (textnum[i] != 0&& textnum[i] !=1)
                {
                    TextBoxLog.AddLog("测试" + (i + 1).ToString() + "箱感应值异常");
                    Stop();
                    return;
                }
                textokng[i] = "no";
            }
            //SetUi(); 

>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
        }
        /// <summary>
        /// 初始化（使状态回到可以运行的状态）
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void initial()
        {
            direction = 1;
            string retul = "";
            retul = getjack();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = satisfy();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = prepose();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = turn(0);
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            Thread.Sleep(500);
            reach();
            Changeled(0);
            Changeled(1);
            Jack();
            Changend();
            //Thread.Sleep(1000);
            retul = prepose();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            retul = turn(1);
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                if (RunState == RunState.Run)
                {
                    Stop();
                }
                return;
            }
            Thread.Sleep(500);
            reach();
            Changeled(0);
            Changeled(1);
            Jack();
            Changend();
        }
        /// <summary>
        /// 获取当前旋转气缸状态（-1通信失败，0两边都没有到位信号，1正向，2反向，3两边都有到位信息）
        /// </summary>
        /// <returns></returns>
        private int getfreque()
        {
            Thread.Sleep(500);
            var dian = LocalStatic.Electric.Readsentfrequency(1000, PortModel.Accurate, LocalFile.Config.Address);
            if (!dian.IsSucceed)
            {
                return -1;
            }
            else
            {
                if (dian.Value == "00 01 ")
                {
                    return 2;
                }
                else if (dian.Value == "01 00 ") {

                    return 1;
                }
                else if (dian.Value == "00 00 ")
                {

                    return 0;
                }
                else
                {

                    return 3;
                }
            }
        }
        /// <summary>
        /// 测试板检测是否有物料
        /// </summary>
        /// <returns></returns>
        private string getjack()
        {
            string retul = "";
            for (int i = 0; i < 4; i++)
            {
<<<<<<< HEAD

=======
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
                var gettext = LocalStatic.Test[0 + i].ReadSensor();
                if (!gettext.IsSucceed)
                {
                    textnum[i] = 2;
                    retul= retul+(i + 1).ToString() + "号测试箱通信失败！\r\n";
                }
                else
                {
                    int getnumber = int.Parse(gettext.Value);
                    if (getnumber < LocalFile.Config.DSensor[i])
                    {
                        textnum[i] = 1;
                    }
                    else if (getnumber > LocalFile.Config.USensor[i])
                    {
                        textnum[i] = 0;
                    }
                    else
                    {
                        textnum[i] = 2;
                        retul = retul + (i + 1).ToString() + "号测试箱未按压到位请按压或拿开！\r\n";
                    }
                }
            }
            if (retul =="") { return "OK"; }
            else { return retul; }
        }
        /// <summary>
        /// 循环线程
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Circulate()
        {
            try {

                //woekt[0] = Task.Run(() => starding());
                starding();
                //Task.WaitAll(woekt[0]);
                while (true)
                {
                    While.Wait(cancellationToken.Token, manualReset);
                    string retul = "";
                    retul = rotate();
                    if (retul != "OK")
                    {
                        TextBoxLog.AddLog(retul);
                        continue;
                    }
                    directioning = true;
                    Task.WaitAll(woektasks);
                    While.Wait(cancellationToken.Token, manualReset);

                    TextBoxLog.AddLog("电流测试完成");
                    reach();
                    //if (retul != "OK")
                    //{
                    //    TextBoxLog.AddLog(retul);
                    //    break;
                    //}
                    directioning = false;
                    SetUi();
                    if (LocalFile.Config.IsAn)
                    {
                        task1[0] = Task.Run(() => Suppress(0));
                        task1[1] = Task.Run(() => Suppress(1));
                        Task.WaitAll(task1);
                    }
                    //Vision(0);
                    //tasks[0] = Task.Run(() => Change());
                    task4[0] = Task.Run(() => Vision(0));
                    task4[1] = Task.Run(() => Vision(1));
                    //task2[0] = Task.Run(() => Changeled(0));
                    //task2[1] = Task.Run(() => Changeled(1));
                    //Task.WaitAll(task2);

                    //While.Wait(cancellationToken.Token, manualReset);
                    //Jack();
                    //Change();
                    Changeled(0);
                    Changeled(1);
                    Jack();
                    //task3[0] = Task.Run(() => Changeliao(0));
                    //task3[1] = Task.Run(() => Changeliao(1));
                    //Task.WaitAll(task3);
                    Changend();
                    TextBoxLog.AddLog("取料完成");
                    While.Wait(cancellationToken.Token, manualReset);
                    starding();
                    Task.WaitAll(task4);
                     SetUi();
                    Updata();

                }
            }
            catch (OperationCanceledException)
            {
                TextBoxLog.AddLog("已停止，主流程");
            }
            catch (Exception ex)
            {
                TextBoxLog.AddLog($"错误：{ex.ToString()}");
                Logs.Write($"错误：{ex.ToString()}", LogsType.Err);
            }
            finally
            {
                cancellationToken?.Cancel();
            }
        }

        private void Updata()
        {
            for (int i=0;i<4;i++) {
                if (textokng[i] == "ok11") {
                    BarChartView.viewModel.AddOk(i+1);
                }
                else if (textokng[i] == "no") 
                {
                    continue;
                }
                else 
                {
                    BarChartView.viewModel.AddNg(i + 1);
                }
            }
        }

        private void Jack()
        {
            var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
            aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
        }
        /// <summary>
        /// 是否点击启动和开始测试第一部分
        /// </summary>
        private void starding()
        {
            runstart = 0;
            Working = false;
            TextBoxLog.AddLog("准备放产品");
            while (true)
            {
                Thread.Sleep(200);
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                string retul = "";
                if (runstart == 0)
                {
                    continue;
                }
                retul = obtain();
                SetUi();
                runstart = 0;
                if (retul != "OK")
                {
                    TextBoxLog.AddLog(retul);
                    continue;
                }
                //DoWork(0);
                woektasks[0] = Task.Run(() => DoWork(0));
                woektasks[1] = Task.Run(() => DoWork(1));
                TextBoxLog.AddLog("开始测试");
                Working = true;
                return;
            }
        }

        /// <summary>
        /// 是否按下气缸，按下的气缸是否顶住
        /// </summary>
        /// <returns></returns>
        private string ready()
        {
            if (direction == 1)
            {
                if (!cylinder) {
                    var ss = LocalStatic.Electric.Readsent(500, "");
                    if (ss.Value == "")
                    {
                        cylinder = true;
                    }
                    else
                    {
                        return "未检测下压";
                    }
                }

            }
            return "OK";
        }
        /// <summary>
        ///按压通断检测
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Suppress(int v)
        {
            string okng = "ng";
            int number = 0;
            int vum;
            if (direction == 2)
            {
                vum = v;
            }
            else
            {
                vum = 2 + v;
            }
            if (textokng[0 + v].Contains("NG") || textokng[0 + v].Contains("ng") || textnum[0 + v] != 1)
            {
                return;
            }
            //if (textnum[0 + v] == 1)
            //{

            var s1 = new Result<string>();
            number = 0;
            while (okng != "ok4")
            {
                number = number + 1;
                if (number > LocalFile.Config.number1)
                {
                    return;
                }

                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                s1 = LocalStatic.Test[0 + v].ReadOnoff();
                if (s1.IsSucceed)
                {
                    if (s1.Value == "")
                    {
                        okng = "ng4";
                    }
                }
                else
                {
                    okng = "NG4";
                }
            }
            number = 0;
            while (okng != "ok")
            {
                number = number + 1;
                if (number > LocalFile.Config.number2)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                s1 = LocalStatic.Test[vum].ReadCurrent();
                if (s1.IsSucceed)
                {
                    int getnumber = int.Parse(s1.Value);
                    TextBoxLog.AddLog((vum) + "UA" + getnumber.ToString());
                    if (getnumber < LocalFile.Config.DUA || getnumber > LocalFile.Config.UUA)
                    {
                        okng = "电流未在范围内";
                    }
                    else
                    {
                        okng = "ok";
                    }
                }
                else
                {
                    okng = "电流测试通信失败";
                }
            }
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vum) + "UA复电失败");
            }
        }
        /// <summary>
        ///视觉检测
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Vision(int v)
        {
            int vnm;//测试位
            string okng="ng";//临时结果
            if (direction == 2)
            {
<<<<<<< HEAD
                vnm = v;
            }
            else
            {
                vnm = v+2;
=======
                vnm = v+2;
            }
            else
            {
                vnm = v;
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
            }

            string retul = "";
            Thread.Sleep(12000);
            int numb = 0;//复测次数
            TextBoxLog.AddLog((v + 1) + "号视觉完成");
            var result = new Result<string>();

            if (textnum[vnm] != 1)
            {
                //SetUi();
                return;
            }
            //else
            //{
            //    textokng[vnm] = "ng";
            //    //SetUi();
            //    //return;
            //}

            if (!textokng[vnm].Contains("ok"))
            {
                return;
            }
            numb = 0;
            while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("进入模式", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                    //return;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok") {
                TextBoxLog.AddLog((vnm+1)+"号测试位进入切换模式失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位进入切换模式成功");
            okng = "ng";
            numb = 0;
            while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面1", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面1视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面1视觉检测成功");
            okng = "ng";
            numb = 0;
            while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面2", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面2视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面2视觉检测成功");
            okng = "ng";
            numb = 0; while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面3", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面3视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面3视觉检测成功");
            okng = "ng";
            numb = 0; while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面4", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面4视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面4视觉检测成功");
            okng = "ng";
            numb = 0;
            while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面5", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面5视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面5视觉检测成功");
            okng = "ng";
            numb = 0; while (okng != "ok")
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面6", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
                    okng = "ok";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面6视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面1视觉检测成功");
            okng = "ng";
<<<<<<< HEAD
            numb = 0; while (okng != "okend")
=======
            numb = 0; while (okng != "ok")
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
            {
                numb = numb + 1;
                if (numb < 10)
                {
                    return;
                }
                if (RunState == RunState.Stop) { return; }
                While.Wait(cancellationToken.Token, manualReset);
                //var ss = getvision(v, PortModel.SAlocation1, PortModel.GAlocation1, PortModel.SBlocation1, PortModel.GBlocation1, PortModel.Vision1);
                retul = getvision1("页面7", vnm);
                if (retul != "OK")
                {
                    okng = retul;
                }
                else
                {
<<<<<<< HEAD
                    okng = "okend";
                }
            }
            textokng[vnm] = okng;
            if (okng != "okend")
=======
                    okng = "ok13";
                }
            }
            textokng[vnm] = okng;
            if (okng != "ok")
>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
            {
                TextBoxLog.AddLog((vnm + 1) + "号测试位页面7视觉检测失败");
                return;
            }
            TextBoxLog.AddLog((vnm + 1) + "号测试位页面7视觉检测成功");
            okng = "ng";
            numb = 0;
            //SetUi();
        }

        /// <summary>
        /// 页面切换加视觉判断
        /// </summary>
        /// <param name="v1">0左1右</param>
        /// <param name="v2">左切换</param>
        /// <param name="v4">右切换</param>
        /// <param name="v3">视觉通信</param>
        /// <returns></returns>
        private string getvision(int v1, byte[] senta, byte[] sentb, byte[] geta, byte[] getb, string v3)
        {
            if (direction == 2)
            {
                if (v1 == 0)
                {
                    var ss = LocalStatic.Change2.Readsent(500, senta);
                    if (!ss.IsSucceed)
                    {
                        return "切换页面通信错误"+ss.Err;
                    }
                    if (ss.Value== Encoding.Default.GetString(geta))
                    {
                        var ioi = LocalStatic.Vision1.SendMes(v3);
                        if (ioi!="")
                        {
                            return "切换页面视觉错误" + ioi;
                        }

                    }
                    else
                    {
                        return "切换页面返回错误" + ss.Value;
                    }
                }
                else if (v1 == 1)
                {
                    var ss = LocalStatic.Change2.Readsent(500, sentb);
                    if (!ss.IsSucceed)
                    {
                        return "切换页面通信错误" + ss.Err;
                    }
                    if (ss.Value == Encoding.Default.GetString(getb))
                    {
                        var ioi = LocalStatic.Vision2.SendMes(v3); 
                        if (ioi != "")
                        {
                            return "切换页面视觉错误" + ioi;
                        }
                    }
                    else
                    {
                        return "切换页面返回错误" + ss.Value;
                    }
                }
                else
                {
                    return "左右错误"+ v1;
                }
            }else if (direction == 1)
            {
                if (v1 == 1)
                {
                    var ss = LocalStatic.Change1.Readsent(500, senta);
                    if (!ss.IsSucceed)
                    {
                        return "切换页面通信错误" + ss.Err;
                    }
                    if (ss.Value == Encoding.Default.GetString(geta))
                    {
                        var ioi = LocalStatic.Vision1.SendMes(v3);
                        if (ioi != "")
                        {
                            return "切换页面视觉错误" + ioi;
                        }
                    }
                    else
                    {
                        return "切换页面返回错误" + ss.Value;
                    }
                }
                else if (v1 == 2)
                {
                    var ss = LocalStatic.Change1.Readsent(500, sentb);
                    if (!ss.IsSucceed)
                    {
                        return "切换页面通信错误" + ss.Err;
                    }
                    if (ss.Value == Encoding.Default.GetString(getb))
                    {
                        var ioi = LocalStatic.Vision2.SendMes(v3);
                        if (ioi != "")
                        {
                            return "切换页面视觉错误" + ioi;
                        }
                    }
                    else
                    {
                        return "切换页面返回错误" + ss.Value;
                    }
                }
                else
                {
                    return "左右错误" + v1;
                }
            }
            else
            {
                return "转盘方向错误" + direction;
            }
            return "OK";
        }
        /// <summary>
        /// 页面切换加视觉判断
        /// </summary>
        /// <param name="msg">测试位+页面</param>
        /// <returns></returns>
        private string getvision1(string msg,int v)
        {
            var result = new Result<string>();//切换页面返回结果
            string getsen = "";//视觉返回结果
            string fangxiang = "";//方向左或右
            int ampip = 0;//安普板IP
            ChangeBan qhaun = LocalStatic.Change1;//切换页面的串口对象
            VisionBan Vision = LocalStatic.Vision2;//视觉对象
            if (v==0) {
                qhaun = LocalStatic.Change1;
                Vision = LocalStatic.Vision2;
                ampip = LocalFile.Config.Amp1;
                fangxiang = "R";
            }
            else if (v == 1)
            {
                qhaun = LocalStatic.Change1;
                Vision = LocalStatic.Vision1;
                ampip = LocalFile.Config.Amp2;
                fangxiang = "L";
            }
            else if (v == 2)
            {
                qhaun = LocalStatic.Change2;
                Vision = LocalStatic.Vision2;
                ampip = LocalFile.Config.Amp3;
                fangxiang = "R";
            }
            else if (v == 3)
            {
                qhaun = LocalStatic.Change2;
                Vision = LocalStatic.Vision1;
                ampip = LocalFile.Config.Amp4;
                fangxiang = "L";
            }
            switch (msg)
            {
                case "进入模式":
                    result = qhaun.Readsent1(500, ampip); 
                    if (!result.IsSucceed)
                    {
                        Thread.Sleep(50);
                    }
                    result = qhaun.Readsent1(500, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面失败" + result.Err;
                    }
                    break;
                case "页面1":
                    result = qhaun.Readsent3(500, ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision1);
                    if (getsen != fangxiang+"CH1;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面2":
                    result = qhaun.Readsent4(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision2);
                    if (getsen != fangxiang + "CH2;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面3":
                    result = qhaun.Readsent5(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision3);
                    if (getsen != fangxiang + "CH3;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面4":
                    result = qhaun.Readsent6(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision4);
                    if (getsen != fangxiang + "CH4;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面5":
                    result = qhaun.Readsent7(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision5);
                    if (getsen != fangxiang + "CH5;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面6":
                    result = qhaun.Readsent8(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision6);
                    if (getsen != fangxiang + "CH6;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
                case "页面7":
                    result = qhaun.Readsent9(2000,  ampip);
                    if (!result.IsSucceed)
                    {
                        return "切换页面视觉错误" + result.Err;
                    }
                    getsen = Vision.SendMes("s=1;x=3;"+ fangxiang + PortModel.Vision7);
                    if (getsen != fangxiang + "CH7;OK")
                    {
                        return "切换页面视觉错误" + getsen;
                    }
                    break;
            }
            return "OK";
        }
        /// <summary>
        /// 换料
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Change()
        {
            if (direction == 1)
            {
                if (textokng[0].Contains("ng")|| textokng[0].Contains("NG"))
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 1, LocalFile.Config.Address);
                }
                else if (textokng[0]== "ok11")
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 1, LocalFile.Config.Address);
                }
                if (textokng[1].Contains("ng"))
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 1, LocalFile.Config.Address);
                }
                else if (textokng[1] == "ok11")
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 1, LocalFile.Config.Address);
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
                Thread.Sleep(500);
                aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
            }
            else
            {
                if (textokng[2].Contains("ng") || textokng[2].Contains("NG"))
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 1, LocalFile.Config.Address);
                }
                else if (textokng[2] == "ok11")
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 1, LocalFile.Config.Address);
                }
                if (textokng[3].Contains("ng") || textokng[3].Contains("NG"))
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 1, LocalFile.Config.Address);
                }
                else if (textokng[3] == "ok11")
                {
                    var dd = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 1, LocalFile.Config.Address);
                }
                var aaa = LocalStatic.Electric.Readsentrelay(500, PortModel.Jack, new List<int>(), LocalFile.Config.Address);
                Thread.Sleep(500);
                aaa = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
            }
            while (true)
            {
                Thread.Sleep(500);
                While.Wait(cancellationToken.Token, manualReset);
                if (direction == 1)
                {
                    if (textnum[0] == 1)
                    {

                        var dd = LocalStatic.Test[0].ReadSensor();
                        if (dd.IsSucceed)
                        {
                            if (!dd.IsSucceed)
                            {

                            }
                            else
                            {
                                int getnumber = int.Parse(dd.Value);
                                if (getnumber < LocalFile.Config.DSensor[0])
                                {
                                    textnum[0] = 1;
                                }
                                else if (getnumber > LocalFile.Config.USensor[0])
                                {
                                    var dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                    dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                                    if (dd1.IsSucceed)
                                    {
                                        textnum[0] = 0;
                                        textokng[0] = "no";
                                    }
                                }
                                else
                                {
                                    textnum[0] = 2;
                                    
                                }
                            }
                        }
                    }
                    if (textnum[1] == 1)
                    {

                        var dd = LocalStatic.Test[1].ReadSensor();
                        if (dd.IsSucceed)
                        {
                            if (!dd.IsSucceed)
                            {

                            }
                            else
                            {
                                int getnumber = int.Parse(dd.Value);
                                if (getnumber < LocalFile.Config.DSensor[1])
                                {
                                    textnum[1] = 1;
                                }
                                else if (getnumber > LocalFile.Config.USensor[1])
                                {
                                    var dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                    dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                                    if (dd1.IsSucceed)
                                    {
                                        textnum[1] = 0;
                                        textokng[1] = "no";
                                    }
                                }
                                else
                                {
                                    textnum[1] = 2;

                                }
                            }

                        }
                    }
                    if (textnum[0] == 0&& textnum[1] == 0)
                    {
                        return;
                    }
                }
                else
                {
                    if (textnum[2] == 1)
                    {

                        var dd = LocalStatic.Test[2].ReadSensor();
                        if (!dd.IsSucceed)
                        {

                        }
                        else
                        {
                            int getnumber = int.Parse(dd.Value);
                            if (getnumber < LocalFile.Config.DSensor[2])
                            {
                                textnum[2] = 1;
                            }
                            else if (getnumber > LocalFile.Config.USensor[2])
                            {
                                textnum[2] = 0;
                                textokng[2] = "no";
                                var dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                            }
                            else
                            {
                                textnum[2] = 2;

                            }
                        }
                    }
                    if (textnum[3] == 1)
                    {

                        var dd = LocalStatic.Test[3].ReadSensor();
                        if (!dd.IsSucceed)
                        {

                        }
                        else
                        {
                            int getnumber = int.Parse(dd.Value);
                            if (getnumber < LocalFile.Config.DSensor[3])
                            {
                                textnum[3] = 1;
                            }
                            else if (getnumber > LocalFile.Config.USensor[3])
                            {
                                textnum[3] = 0;
                                textokng[3] = "no";
                                var dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                            }
                            else
                            {
                                textnum[3] = 2;

                            }
                        }
                    }
                    if (textnum[2] == 0 && textnum[3] == 0)
                    {
                        return;
                    }
                }
            }
        }
        /// <summary>
        /// 换料new亮灯
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Changeled(int v)
        {
            int led = 0;
<<<<<<< HEAD
            int vnum;
            string ngok = "";
            if (direction == 1)
            { vnum = v; 
            }
            else
            {
                vnum = 2 + v;
            }
            if (!textokng[vnum].Contains("no")&& textokng[vnum] != "okend")
            {
                ngok = "ng";
                BarChartView.viewModel.AddNg(vnum + 1);
            }
            else if (textokng[vnum] == "okend")
            {
                ngok = "ok";
                BarChartView.viewModel.AddOk(vnum + 1);

            }
            //}
            //else
            //{
            //    if (textokng[2 + v].Contains("ng") || textokng[2 + v].Contains("NG"))
            //    {
            //        ngok = "ng";
            //    }
            //    else if (textokng[2 + v] == "ok11")
            //    {
            //        ngok = "ok";
            //    }
            //}
            if (v == 0)
            {
                if (ngok == "ok")
                {
                    led = PortModel.LED1ok;
                }
                else if(ngok == "ng")
                {
                    led = PortModel.LED1ng;
                }
            }
            else
            {
                if (ngok == "ok")
                {
                    led = PortModel.LED2ok;
                }
                else if (ngok == "ng")
                {
                    led = PortModel.LED2ng;
                }
            }
            if (ngok == "")
            {
                return;
            }
            var dd = LocalStatic.Electric.Readsentled(500, led, 1, LocalFile.Config.Address);

=======
            string ngok = "";
            if (direction == 1)
            {
                if (textokng[0+v].Contains("ng") || textokng[0 + v].Contains("NG"))
                {
                    ngok = "ng";
                }
                else if (textokng[0 + v] == "ok11")
                {
                    ngok = "ok";
                }
            }
            else
            {
                if (textokng[2 + v].Contains("ng") || textokng[2 + v].Contains("NG"))
                {
                    ngok = "ng";
                }
                else if (textokng[2 + v] == "ok11")
                {
                    ngok = "ok";
                }
            }
            if (v==0)
            {
                if (ngok == "ok")
                {
                    led = PortModel.LED1ok;
                }
                else
                {
                    led = PortModel.LED1ng;
                }
            }
            else
            {
                if (ngok == "ok")
                {
                    led = PortModel.LED2ok;
                }
                else
                {
                    led = PortModel.LED2ng;
                }
            }
            if (ngok=="")
            {
                return;
            }
            var dd = LocalStatic.Electric.Readsentled(500, led, 1, LocalFile.Config.Address);

>>>>>>> 44c8a28dc53dc5cd9c73229860834f726801c071
        }
        /// <summary>
        /// 换料new
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Changeliao(int v)
        {
            var qhuan = new Result<string>();

            while (true)
            {
                Thread.Sleep(200);
                if (RunState == RunState.Stop) {
                    var dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                    dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                    dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                    dd1 = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                    return; }
                While.Wait(cancellationToken.Token, manualReset);
                if (direction == 1)
                {
                    if (textnum[0+v] != 0)
                    {

                        var dd = LocalStatic.Test[0 + v].ReadSensor();
                        if (dd.IsSucceed)
                        {
                            if (!dd.IsSucceed)
                            {

                            }
                            else
                            {
                                int getnumber = int.Parse(dd.Value);
                                if (getnumber > LocalFile.Config.USensor[0 + v])
                                {
                                    if (v == 0)
                                    {
                                        if (textokng[0 + v].Contains("ng") || textokng[0 + v].Contains("NG"))
                                        {
                                            qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                                        }
                                        else
                                        {
                                            qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                        }
                                    }
                                    else
                                    {
                                        if (textokng[0 + v].Contains("ng") || textokng[0 + v].Contains("NG"))
                                        {
                                            qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                                        }
                                        else
                                        {
                                            qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                        }
                                    }
                                    if (qhuan.IsSucceed)
                                    {
                                        textnum[0 + v] = 0;
                                        textokng[0 + v] = "no";
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (textnum[2 + v] != 0)
                    {

                        var dd = LocalStatic.Test[2 + v].ReadSensor();
                        if (!dd.IsSucceed)
                        {

                        }
                        else
                        {
                            int getnumber = int.Parse(dd.Value);

                            if (getnumber > LocalFile.Config.USensor[2 + v])
                            {
                                if (v == 0)
                                {
                                    if (textokng[2 + v].Contains("ng") || textokng[2 + v].Contains("NG"))
                                    {
                                        qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                                    }
                                    else
                                    {
                                        qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                    }
                                }
                                else
                                {
                                    if (textokng[2 + v].Contains("ng") || textokng[2 + v].Contains("NG"))
                                    {
                                        qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                                    }
                                    else
                                    {
                                        qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                    }
                                }
                                if (qhuan.IsSucceed)
                                {
                                    textnum[2 + v] = 0;
                                    textokng[2 + v] = "no";
                                    return;
                                }
                            }
                            else
                            {


                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 换料最终
        /// </summary>
        /// <param name="v"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Changend()
        {
            var qhuan = new Result<string>();

            while (true)
            {
                Thread.Sleep(500);
                While.Wait(cancellationToken.Token, manualReset);
                if (direction == 1)
                {
                    if (textnum[0] == 0&&textnum[1] == 0)
                    {
                        return;
                    }
                    if (textnum[0] != 0)
                    {

                        var dd = LocalStatic.Test[0].ReadSensor();
                        if (dd.IsSucceed)
                        {

                            int getnumber = int.Parse(dd.Value);
                            if (getnumber > LocalFile.Config.USensor[0])
                            {

                                if (textokng[0].Contains("ng") || textokng[0].Contains("NG"))
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                                }
                                else
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                }

                                if (qhuan.IsSucceed)
                                {
                                    textnum[0] = 0;
                                    textokng[0] = "no";
                                }
                            }

                        }
                    }
                    if (textnum[1] != 0)
                    {

                        var dd = LocalStatic.Test[1].ReadSensor();
                        if (dd.IsSucceed)
                        {

                            int getnumber = int.Parse(dd.Value);
                            if (getnumber > LocalFile.Config.USensor[1])
                            {

                                if (textokng[1].Contains("ng") || textokng[1].Contains("NG"))
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                                }
                                else
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                }

                                if (qhuan.IsSucceed)
                                {
                                    textnum[1] = 0;
                                    textokng[1] = "no";
                                }
                            }

                        }
                    }
                }
                else
                {
                    if (textnum[2] == 0 && textnum[3] == 0)
                    {
                        return;
                    }
                    if (textnum[2] != 0)
                    {

                        var dd = LocalStatic.Test[2].ReadSensor();
                        if (dd.IsSucceed)
                        {

                            int getnumber = int.Parse(dd.Value);
                            if (getnumber > LocalFile.Config.USensor[2])
                            {

                                if (textokng[2].Contains("ng") || textokng[2].Contains("NG"))
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ng, 0, LocalFile.Config.Address);
                                }
                                else
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED1ok, 0, LocalFile.Config.Address);
                                }

                                if (qhuan.IsSucceed)
                                {
                                    textnum[2] = 0;
                                    textokng[2] = "no";
                                }
                            }

                        }
                    }
                    if (textnum[3] != 0)
                    {

                        var dd = LocalStatic.Test[3].ReadSensor();
                        if (dd.IsSucceed)
                        {

                            int getnumber = int.Parse(dd.Value);
                            if (getnumber > LocalFile.Config.USensor[3])
                            {

                                if (textokng[3].Contains("ng") || textokng[3].Contains("NG"))
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ng, 0, LocalFile.Config.Address);
                                }
                                else
                                {
                                    qhuan = LocalStatic.Electric.Readsentled(500, PortModel.LED2ok, 0, LocalFile.Config.Address);
                                }

                                if (qhuan.IsSucceed)
                                {
                                    textnum[3] = 0;
                                    textokng[3] = "no";
                                }
                            }

                        }
                    }
                }
            }
        }
        /// <summary>
        /// 转盘是否到位
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private void reach()
        {
            int olddirection = direction;
            int i = 0;
            while(true)
            {
                i = i + 1;
                While.Wait(cancellationToken.Token, manualReset);
                var de = LocalStatic.Electric.Readsentfrequency(500, PortModel.Accurate, LocalFile.Config.Address);

                if (de.IsSucceed)
                {
                    if (de.Value == "00 01 "&& olddirection!= 2)
                    {
                        direction = 2;
                        return;
                    }
                    else if (de.Value == "01 00 " && olddirection != 1)
                    {

                        direction = 1;
                        return;
                    }
                    else if (de.Value == "00 00 ")
                    {

                        direction = 0;
                    }
                    else
                    {

                        direction = 3;
                    }


                }
                if (i>100)
                {
                    i = 0;
                    TextBoxLog.AddLog("转盘超时！暂停");
                    Pause();
                }
                Thread.Sleep(100);
            }
        }
        /// <summary>
        /// 转盘是否到位strinf
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string reach1()
        {
            int i = 0;
            while (true)
            {
                i = i + 1;
                While.Wait(cancellationToken.Token, manualReset);
                var de = LocalStatic.Electric.Readsentfrequency(500, PortModel.Accurate, LocalFile.Config.Address);

                if (de.IsSucceed)
                {
                    if (de.Value == "00 01 ")
                    {
                        direction = 2;
                    }
                    else if (de.Value == "01 00 ")
                    {

                        direction = 1;
                    }
                    else if (de.Value == "00 00 ")
                    {

                        direction = 0;
                        return "旋转中！";
                    }
                    else
                    {

                        direction = 3;
                        return "两边到位！";
                    }

                    return "OK";
                }

                return "转盘超时！";
            }
            Thread.Sleep(100);
        }

        /// <summary>
        /// 在旋转时就开始测试
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void DoWork(int i)
        {
            string okng = "ng";//临时结果
            int number = 0;//复测次数
            int vum = 0;//测试位
            if (direction == 1)
            {
                vum = i;
            }
            else
            {
                vum = i + 2;
            }

            if (textnum[vum] == 1)
            {
                var s1 = new Result<string>();
                number = 0;
                while (okng != "ok")
                {
                    number = number + 1;
                    if (number > LocalFile.Config.number1)
                    {
                        return;
                    }
                    if (RunState == RunState.Stop) { return; }
                    While.Wait(cancellationToken.Token, manualReset);

                    s1 = LocalStatic.Test[vum].ReadOnoff();
                    if (s1.IsSucceed)
                    {
                        string restul = judge(s1.Value);

                        //TextBoxLog.AddLog((vum) + "UA" + getnumber.ToString());
                        if (restul != "OK")
                        {
                            okng = restul;
                        }
                        else
                        {
                            okng = "ok";
                        }

                    }
                    else
                    {
                        okng = "通断通信失败" + s1.Value;
                    }
                }
                textokng[vum] = okng;
                if (okng != "ok")
                {
                    TextBoxLog.AddLog((vum) + "号测试位通断测试失败");
                    return;
                }
                okng = "ng";
                TextBoxLog.AddLog((vum) + "号测试位通断测试完成");
                number = 0;
                while (okng != "ok")
                {
                    number = number + 1;
                    if (number > LocalFile.Config.number2)
                    {
                        return;
                    }
                    if (RunState == RunState.Stop) { return; }
                    While.Wait(cancellationToken.Token, manualReset);
                    s1 = LocalStatic.Test[vum].ReadFrequency();
                    if (s1.IsSucceed)
                    {
                        int getnumber = int.Parse(s1.Value);

                        TextBoxLog.AddLog((vum) + "HZ" + getnumber.ToString());
                        if (getnumber < LocalFile.Config.DHZ || getnumber > LocalFile.Config.UHZ)
                        {
                            okng = i + "频率未在范围内";
                        }
                        else
                        {
                            okng = "ok";
                        }

                    }
                    else
                    {
                        okng = i + "频率通信失败";
                    }
                }
                textokng[vum] = okng;
                if (okng != "ok")
                {
                    TextBoxLog.AddLog((vum) + "号测试位频率测试失败");
                    return;
                }
                okng = "ng";
                TextBoxLog.AddLog((vum) + "号测试位频率测试完成");
                number = 0;
                while (okng != "ok")
                {
                    number = number + 1;
                    if (number > LocalFile.Config.number2)
                    {
                        return;
                    }
                    if (RunState == RunState.Stop) { return; }
                    While.Wait(cancellationToken.Token, manualReset);
                    s1 = LocalStatic.Test[vum].ReadCurrent();
                    if (s1.IsSucceed)
                    {
                        int getnumber = int.Parse(s1.Value);
                        TextBoxLog.AddLog((vum) + "UA" + getnumber.ToString());
                        if (getnumber < LocalFile.Config.DUA || getnumber > LocalFile.Config.UUA)
                        {
                            okng = "电流未在范围内";
                        }
                        else
                        {
                            okng = "ok";
                        }
                    }
                    else
                    {
                        okng = "电流测试通信失败";
                    }
                }
                textokng[vum] = okng;
                if (okng != "ok")
                {
                    TextBoxLog.AddLog((vum) + "号测试位电流测试失败");
                    return;
                }
                okng = "ng";
                TextBoxLog.AddLog((vum) + "号测试位电流测试完成");
            }

        }
        /// <summary>
        /// 通断检测
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private string judge(string value)
        {
            return "OK";
            string[] msg = value.Split(' ');
            if(int.Parse(msg[0])> LocalFile.Config.ULCDCLK|| int.Parse(msg[0]) < LocalFile.Config.DLCDCLK)
            {
                return "LCDCLK超出范围";
            }
            if (int.Parse(msg[1]) > LocalFile.Config.UVCC || int.Parse(msg[0]) < LocalFile.Config.DVCC)
            {
                return "VCC超出范围";
            }
            if (int.Parse(msg[2]) > LocalFile.Config.UVDD || int.Parse(msg[0]) < LocalFile.Config.DVDD)
            {
                return "VDD超出范围";
            }
            if (int.Parse(msg[3]) > LocalFile.Config.UGND || int.Parse(msg[0]) < LocalFile.Config.DGND)
            {
                return "GND超出范围";
            }
            if (int.Parse(msg[4]) > LocalFile.Config.USW || int.Parse(msg[0]) < LocalFile.Config.DSW)
            {
                return "SW超出范围";
            }
            if (int.Parse(msg[5]) > LocalFile.Config.ULCDDAT || int.Parse(msg[0]) < LocalFile.Config.DLCDDAT)
            {
                return "LCDDAT超出范围";
            }
            if (int.Parse(msg[6]) > LocalFile.Config.UNON || int.Parse(msg[0]) < LocalFile.Config.DNON)
            {
                return "NON超出范围";
            }
            if (int.Parse(msg[7]) > LocalFile.Config.ULCDSTB || int.Parse(msg[0]) < LocalFile.Config.DLCDSTB)
            {
                return "LCDSTB超出范围";
            }
            return "OK";
        }

        /// <summary>
        /// 根据转盘方向反方向旋转
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string rotate()
        {
            string retul = prepose();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                Pause();
            }
            if (direction == 2)
            {
                var ioi1 = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
                if (!ioi1.IsSucceed)
                {
                    return ioi1.Err;
                }
                way = 1;
                return "OK";
            }
            else if (direction == 1)
            {
                var ioi3 = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
                if (!ioi3.IsSucceed)
                {
                    return ioi3.Err;
                }
                way = 2;
                return "OK";
            }
            else
            {
                way = 0;
                return "转盘状态异常" + direction.ToString();
            }
        }
        /// <summary>
        /// 是否可以旋转
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string prepose()
        {
            var ioi = LocalStatic.Electric.Readsentfrequency(500, PortModel.EAll, LocalFile.Config.Address);
            if (!ioi.IsSucceed)
            {
                return ioi.Err;
            }
            else
            {

                string[] rest = ioi.Value.Split(' ');

                //var send = SetTrigger( LocalFile.Config.Address);
                if (ioi.Value.Length != 42)
                {
                    return "长度错误";
                }
                if (rest[2] != "01")
                {
                    return "顶升1-1顶断开未检测到位" + rest[2];
                }

                if (rest[3] != "00")
                {
                    return "顶升1-2顶闭合异常" + rest[3];
                }
                if (rest[4] != "01")
                {
                    return "顶升2-1顶断开未检测到位" + rest[4];
                }
                if (rest[5] != "00")
                {
                    return "顶升2-2顶闭合异常" + rest[5];
                }

                if (rest[6] != "00")
                {
                    //return "开关箱报警";
                }
                if (rest[8] != "00")
                {
                    return "急停按钮报警";
                }
                if (rest[9] != "00")
                {
                    //return "光栅报警";
                }
            }
            return "OK";
        }
        /// <summary>
        /// 旋转
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string turn(int v)
        {
            var ioi = new Result<string>();
            if (v == 0)
            {
                ioi = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate2, PortModel.Rotate1, LocalFile.Config.Address);
            }
            else
            {
                ioi = LocalStatic.Electric.Readsentrelay(500, PortModel.Rotate1, PortModel.Rotate2, LocalFile.Config.Address);
            }
            
            if (!ioi.IsSucceed)
            {
                return "旋转失败"+ioi.Err;
            }
            return "OK";
        }

        /// <summary>
        /// 顶起和按压断开
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string satisfy()
        {
            var ioi = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Jack, LocalFile.Config.Address);
            if (!ioi.IsSucceed)
            {
                return "顶升气缸下降通信失败"+ioi.Err;
            }
            ioi = LocalStatic.Electric.Readsentrelay(500, new List<int>(), PortModel.Suppress, LocalFile.Config.Address);
            if (!ioi.IsSucceed)
            {
                return "按压气缸上升通信失败" + ioi.Err;
            }

            return "OK";
        }
        /// <summary>
        /// 串口通信强制启动还是正常启动
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string obtain()
        {
            //var ioi11 = LocalStatic.Electric.Readsent(500,"");
            if (direction == 1)
            {
                var ioi1 = LocalStatic.Test[0].ReadSensor();
                if (!ioi1.IsSucceed)
                {
                    return "1号测试箱通信失败！";
                }

                int getnumber = int.Parse(ioi1.Value);
                if (getnumber < LocalFile.Config.DSensor[0])
                {
                    textnum[0] = 1;
                }
                else if (getnumber > LocalFile.Config.USensor[0])
                {
                    textnum[0] = 0;
                }
                else
                {
                    textnum[0] = 2;
                    return (0 + 1).ToString() + "号测试箱感应器数值异常！" + getnumber;
                }
                ioi1 = LocalStatic.Test[1].ReadSensor();
                if (!ioi1.IsSucceed)
                {
                    return "2号测试箱通信失败！";
                }

                getnumber = int.Parse(ioi1.Value);
                if (getnumber < LocalFile.Config.DSensor[1])
                {
                    textnum[1] = 1;
                }
                else if (getnumber > LocalFile.Config.USensor[1])
                {
                    textnum[1] = 0;
                }
                else
                {
                    textnum[1] = 2;
                    return (1 + 1).ToString() + "号测试箱感应器数值异常！" + getnumber;
                }
            }
            else if (direction == 2)
            {
                var ioi3 = LocalStatic.Test[2].ReadSensor();
                if (!ioi3.IsSucceed)
                {
                    return "3号测试箱通信失败！";
                }

                int getnumber = int.Parse(ioi3.Value);
                if (getnumber < LocalFile.Config.DSensor[2])
                {
                    textnum[2] = 1;
                }
                else if (getnumber > LocalFile.Config.USensor[2])
                {
                    textnum[2] = 0;
                }
                else
                {
                    textnum[2] = 2;
                    return (2 + 1).ToString() + "号测试箱感应器数值异常！" + getnumber;
                }
                ioi3 = LocalStatic.Test[3].ReadSensor();
                if (!ioi3.IsSucceed)
                {
                    return "4号测试箱通信失败！";
                }

                getnumber = int.Parse(ioi3.Value);
                if (getnumber < LocalFile.Config.DSensor[3])
                {
                    textnum[3] = 1;
                }
                else if (getnumber > LocalFile.Config.USensor[3])
                {
                    textnum[3] = 0;
                }
                else
                {
                    textnum[3] = 2;
                    return (3 + 1).ToString() + "号测试箱感应器数值异常！" + getnumber;
                }
            }
            else
            {
                return "转盘状态异常" + direction.ToString();
            }
            //是否强制启动
            if (runstart == 1)
            {
                //正常启动启动判断是否满足条件
                if (direction == 1)
                {
                    if (textnum[0] != 1)
                    {
                        return "1号位错误" + textnum[0].ToString();
                    }
                    if (textnum[1] != 1)
                    {
                        return "2号位错误" + textnum[1].ToString();
                    }
                    return "OK";
                }
                else
                {
                    if (textnum[2] != 1)
                    {
                        return "3号位错误" + textnum[2].ToString();
                    }
                    if (textnum[3] != 1)
                    {
                        return "4号位错误" + textnum[3].ToString();
                    }
                    return "OK";
                }
            }
            else if (runstart == 2)
            {
                return "OK";
            }
            else
            {
                return "启动状态错误"+ runstart;
            }
        }
        public void qidong() {
            string retul;
            retul = reach1();
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                return;
            }
            retul = obtain();
            //SetUi();
            runstart = 0;
            if (retul != "OK")
            {
                TextBoxLog.AddLog(retul);
                return;
            }
        }
        /// <summary>
        /// 开始
        /// </summary>
        private void Start()
        {
            cancellationToken = new CancellationTokenSource();
            manualReset = new ManualResetEvent(true);
            //Task.Run(TaskAll);
            RunState = RunState.Run;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            manualReset.Reset();
            RunState = RunState.Pause;
        }

        /// <summary>
        /// 继续
        /// </summary>
        public void Continue()
        {
            manualReset.Set();
            RunState = RunState.Run;
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            cancellationToken.Cancel();
            RunState = RunState.Stop;
        }

        private void TaskAll()
        {

            try
            {
                Task.WaitAll(Task1(), Task2());
            }
            catch (Exception ex)
            {
                TextBoxLog.AddLog($"错误：{ex.ToString()}");
            }
            finally
            {
                Stop();
                TextBoxLog.AddLog("已停止：全部");
            }
        }

        private async Task Task1()
        {
            try
            {
                for (int i = 0; true; i++)
                {
                    await Task.Delay(500);
                    //监听：开始、暂停、停止
                    While.Wait(cancellationToken.Token, manualReset);

                    TextBoxLog.AddLog($"任务1：{i}");
                }
            }
            catch (OperationCanceledException)
            {
                TextBoxLog.AddLog("已停止1");
            }
            catch (Exception ex)
            {
                TextBoxLog.AddLog($"错误：{ex.ToString()}");
            }
            finally
            {
                cancellationToken?.Cancel();
            }
        }

        private async Task Task2()
        {
            try
            {
                for (int i = 0; true; i++)
                {
                    await Task.Delay(500);
                    //监听：开始、暂停、停止
                    While.Wait(cancellationToken.Token, manualReset);

                    TextBoxLog.AddLog($"任务2：{i}");
                }
            }
            catch (OperationCanceledException)
            {
                TextBoxLog.AddLog("已停止2");
            }
            catch (Exception ex)
            {
                TextBoxLog.AddLog($"错误：{ex.ToString()}");
            }
            finally
            {
                cancellationToken?.Cancel();
            }
        }
        #endregion
    }
}
